//
//  FirstVC.m
//  Doctor
//
//  Created by 阳光 on 16/10/21.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "FirstVC.h"
#import "CountryListModel.h"
#import "AreaListVC.h"

static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";

@interface FirstVC ()<UITableViewDataSource,UITableViewDelegate,CustomNavViewDelegate>

@property(nonatomic,strong)UITableView *countryListTableView;

@property(nonatomic,strong)NSMutableArray *countryListArr;

@end

@implementation FirstVC

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)rightBtnEvent
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _countryListArr=[NSMutableArray array];
    
    CustomNavView *countrylistNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Country for hospital", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"选择所在国家"
    countrylistNavView.delegate=self;
    countrylistNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:countrylistNavView];
    
    // 获取国家列表数据
    [self getCountryListData];
    
    [self initTableView];
    
}

// 获取国家列表数据
-(void)getCountryListData
{
    [self.countryListArr removeAllObjects];
    
    __weak typeof(self) weakSelf=self;
    
//    [QBRequest GetCountryInfoWithDic:nil Success:^(id res)
//     {
//         NSLog(@"209923093282--%@",res);
//         
//         weakSelf.countryListArr=[CountryListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
//         
//         [weakSelf.countryListTableView reloadData];
//         
//         [weakSelf.countryListTableView.mj_header endRefreshing];
//         
//     } failure:^(NSString *error)
//     {
//         [self.view makeToast:error duration:1.0 position:CSToastPositionCenter];
//         [weakSelf.countryListTableView.mj_header endRefreshing];
//     }];
}

-(void)initTableView
{
    // _countryListTableView
    _countryListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,64,APPW,APPH-64) style:UITableViewStylePlain];
    _countryListTableView.delegate=self;
    _countryListTableView.dataSource=self;
//    _countryListTableView.hidden=NO;
    _countryListTableView.backgroundColor=WhiteColor;
    _countryListTableView.showsVerticalScrollIndicator=NO;
    _countryListTableView.tableFooterView=[UIView new];
    _countryListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_countryListTableView];
    [_countryListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.countryListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    
    CountryListModel *countryListModel=self.countryListArr[indexPath.row];
    
    BOOL isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];
    if (isCN == YES)
    {
        cell.textLabel.text = countryListModel.countryname_cn;
    }
    else
    {
        cell.textLabel.text = countryListModel.countryname_en;
    }
    
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100/2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    CountryListModel *countryListModel=self.countryListArr[indexPath.row];
    AreaListVC *areaListVC=[AreaListVC new];
    areaListVC.countryID=countryListModel.countryid;
    [self presentViewController:areaListVC animated:NO completion:nil];
    
}

@end
